<?php
define("STDCALL",true);
$request = $_SERVER['QUERY_STRING'];

if(empty ($request)) $request = "main/cMain";
//parse the page request and other GET variables
//URL will look like index.php?[app folder]/[controller name]/[variable]=[value]
$parsed = explode('/', $request);

//1st segment is appliccation folder
//2nd segment is name of the controllers
//3rd segment is the GET variable
$folder = $parsed[0];
$page = $parsed[1];

function __autoload($className)
{   
    global $folder;
    
    //parse out filename where class should be located
    list($filename , $suffix) = split('_' , $className);
    
    //compose file name
    $file = SERVER_ROOT . 'app/' . $folder . '/m/' . $filename . '.php';

    //fetch file
    if (file_exists($file))
    {
        //get file
        include_once($file);        
    }
    else
    {
        //file does not exist!
        die("File '$filename' containing class '$className' not found. file ".$file);    
    }
}

//split variable with &
$getVariable = explode("&", $parsed[2]);

$getVars = array();
if(!empty($getVariable)) {
    foreach ($getVariable as $argument) {
        //split GET vars along '=' symbol to separate variable, values
        list($variable, $value) = split('=', $argument);
        $getVars[$variable] = $value;
    }
}

//file target
$target = SERVER_ROOT . 'app/' . $folder . '/c/' . $page . '.php';

if (file_exists($target)) {
    include_once($target);
    $class = $page;
    if (class_exists($class)) {
        $controller = new $class;
    } else {
        //did we name our class correctly?
        die('class does not exist!');
    }
} else {
    //can't find the file in 'controllers'! 
    die('page ' . $target . ' does not exist!');
}
//the rest of the array are get statements, parse them out.

//the rest of action will taken on main method on each class
$controller->main($getVars);
?>
